package com.itboye.lottery.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.itboye.lottery.BuildConfig
import com.itboye.lottery.MainActivity
import com.itboye.lottery.R
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.app.MyApplication
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.bean.UserDataBean
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import com.itboye.lottery.utils.*
import com.itboye.lottery.widget.titlebarwidget.TranslucentActionBar
import com.tencent.mm.opensdk.modelmsg.SendAuth
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity(), ResponseCallback {


    var rl_wx_login: RelativeLayout? = null
    var userPresenter: UserPresenter? = null
    var broadCast: BroadCast? = null
    var tv_get_yzm: TextView? = null
    var timeCount: TimeCount? = null
    var tv_register: TextView? = null
    var head_title: TranslucentActionBar? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initData() {
        userPresenter = UserPresenter(this)
        timeCount = TimeCount(30000, 1000, tv_get_yzm)
        rl_invite_code.visibility = View.GONE
        head_title!!.visibility=View.GONE
        iv_logo.setBackgroundResource(Flavor().logo)
        broadCast = BroadCast()
        var intentFilter = IntentFilter(userPresenter!!.WX_LOGIN_SUCCESS)
        registerReceiver(broadCast, intentFilter)
    }

    class BroadCast : BroadcastReceiver() {
        override fun onReceive(p0: Context?, p1: Intent?) {
            (p0 as LoginActivity).finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(broadCast)
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.rl_wx_login -> {
//                if (BuildConfig.IS_DEBUG) {
////                    userPresenter!!.wxLogin("wx_code",i);
//                    wxLogin()
//                } else {
//                    wxLogin()
//                }
                //手机号码登录
                var phone = et_phone.text.toString()
                var code = et_code.text.toString()
                if (!Utils.emptyMsgShow(phone, "手机号码")) return
                if (!Utils.emptyMsgShow(code, "验证码")) return
                showDialog("登录中")
                userPresenter!!.loginByPhone(phone, code)
            }
            R.id.tv_get_yzm -> {
                //获取验证码
                var phone = et_phone.text.toString()
                if (!Utils.emptyMsgShow(phone, "手机号码")) return
                timeCount!!.start()
                userPresenter!!.sendCode(8, phone)
            }
            R.id.tv_register -> {
                //注册
                startActivity(Intent(this, RegisterActivity::class.java))
            }
            else -> {
            }
        }
    }

    private fun wxLogin() {
        var req = SendAuth.Req()
        req.scope = "snsapi_userinfo"
        req.state = "wechat_sdk_demo_test"
        req.transaction = Const.WX_LOGIN
        MyApplication.Companion.instance!!.iwxapi!!.sendReq(req)
    }


    override fun success(o: Any, requestSuccessType: String) {
        try {
            dissDialog()
        }catch (e:Exception){

        }
        if (requestSuccessType == userPresenter!!.loginByPhone_SUCCESS) {
            ByAlert.alert(getString(R.string.login_success))
            val userDataBean = o as UserDataBean
            SPUtils.put(this, userDataBean)
//            val intent = Intent(WX_LOGIN_SUCCESS)
//            sendBroadcast(intent)
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        } else if (requestSuccessType == userPresenter!!.sendCode_SUCCESS) {
            ByAlert.alert(o)
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        try {
            dissDialog()
        }catch (e:Exception){

        }
        if (requestErrorType === userPresenter!!.WX_LOGIN_FAIL) {
            ByAlert.alert(msg)
        } else if (requestErrorType === userPresenter!!.loginByPhone_FAIL) {
            ByAlert.alert(msg)
        }
    }
}
